{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting U.S. marriage and divorce statistics\n",
    "\n",
    "Example code by [Randal S. Olson](http://www.randalolson.com)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bokeh.io import show, output_notebook\n",
    "from bokeh.models import HoverTool, NumeralTickFormatter, SingleIntervalTicker, LinearAxis, ColumnDataSource\n",
    "from bokeh.plotting import figure\n",
    "from bokeh.sampledata.us_marriages_divorces import data\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "md_data = data.copy()\n",
    "\n",
    "# Fill in missing data with a simple linear interpolation\n",
    "md_data = md_data.interpolate(method='linear', axis=0).ffill().bfill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Set up the data sources for the lines we'll be plotting.\n",
    "# We need separate data sources for each line because we're\n",
    "# displaying different data in the hover tool.\n",
    "\n",
    "source_marriages = ColumnDataSource(\n",
    "    data=dict(\n",
    "        # x-axis (Years) for the chart\n",
    "        x=md_data.Year.values,\n",
    "        # y-axis (Marriages per capita) for the chart\n",
    "        y=md_data.Marriages_per_1000.values,\n",
    "        # The string version of the y-value that is displayed in the hover box\n",
    "        y_text=md_data.Marriages_per_1000.apply(lambda x: '{}'.format(round(x, 1))),\n",
    "        # Extra descriptive text that is displayed in the hover box\n",
    "        desc=['marriages per 1,000 people'] * len(md_data),\n",
    "    )\n",
    ")\n",
    "\n",
    "source_divorces = ColumnDataSource(\n",
    "    data=dict(\n",
    "        # x-axis (Years) for the chart\n",
    "        x=md_data.Year.values,\n",
    "        # y-axis (Marriages per capita) for the chart\n",
    "        y=md_data.Divorces_per_1000.values,\n",
    "        # The string version of the y-value that is displayed in the hover box\n",
    "        y_text=md_data.Divorces_per_1000.apply(lambda x: '{}'.format(round(x, 1))),\n",
    "        # Extra descriptive text that is displayed in the hover box\n",
    "        desc=['divorces and annulments per 1,000 people'] * len(md_data),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Use HTML to mark up the tooltip that displays over the chart\n",
    "# Note that the variables in the data sources (above) are referenced with a @\n",
    "hover = HoverTool(tooltips='<font face=\"Arial\" size=\"3\">@y_text @desc in @x</font>', mode='vline')\n",
    "\n",
    "# Select the tools that will be available to the chart\n",
    "TOOLS = ['pan,wheel_zoom,box_zoom,reset,save'] + [hover]\n",
    "\n",
    "bplot = figure(\n",
    "    tools=TOOLS, width=800, height=400, x_axis_type=None, \n",
    "    title='144 years of marriage and divorce in the U.S.',\n",
    "    responsive=True\n",
    ")\n",
    "\n",
    "# Create a custom x-axis with 10-year intervals\n",
    "ticker = SingleIntervalTicker(interval=10, num_minor_ticks=0)\n",
    "xaxis = LinearAxis(ticker=ticker)\n",
    "bplot.add_layout(xaxis, 'below')\n",
    "\n",
    "# Customize the y-axis\n",
    "bplot.yaxis.formatter = NumeralTickFormatter(format='0.0a')\n",
    "bplot.yaxis.axis_label = '# per 1,000 people'\n",
    "\n",
    "# Finally, plot the data!\n",
    "# Note that the data source determines what is plotted and what shows in the tooltips\n",
    "bplot.line('x', 'y', color='#1f77b4', line_width=3, source=source_marriages)\n",
    "bplot.line('x', 'y', color='#ff7f0e', line_width=3, source=source_divorces)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "show(bplot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
